%% Onoma:MYLONAS ATHANASIOS
%% AEM:909
%% ergasia 4 - meros I
%% mulonas@inf.uth.gr



%diary thanos

%% askisi 1
% disp('ASKHSH 1');
% a=[2 1 1]
% b=[-1 1 1]
% 
% esot_gin=a*b'

%%eukola diakrinoume oti to b opos kai kathe pollaplasio tou einai orthogonio
%%sto a.to mhdeniko dianisma einai epishs orthogonio sto a.
%%to dianisma [1 2 -4] opos kai ta pollaplasia tou einai episis orthogonia
%%sto a

%% askisi 2
% disp('ASKHSH 2');
% a = [2 1 1]';
% b = [1 2 -4]';
% c = cross(a,b);
% figure(2);
% clf;
% drawVector([a b c], 'r.', {'a','b','c'});
% view(-160, 7)

%% askisi 3
% disp('ASKHSH 3');
% A = [a b c];
% X = sampleUnitSphere(3,200);
% AX = A*X;
% figure(1); clf; 
% hold on;
% plot3( X(1,:), X(2,:), X(3,:),'b.');
% view(-60,10)
% plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
% drawVector(A, 'r.') 
% hold off; 
% axis square equal; 
% grid on;

%% askisi 4
% disp('ASKHSH 4');
% B = [a b c]
% C = diag([1/norm(a) 1/norm(b) 1/norm(c)]) 
% A = B*C

%% askisi 5
% disp('ASKHSH 5');
% A=[1 -1; 1 1]
% 
% A(:,1)
% A(:,2)
% 
%%o antistrofos einai o pinakas B=[1 1; -1 1]

%% 
%%     NORMES
% disp('NORMES');
%%
%% askisi 1
% disp('ASKHSH 1');
% A = [-2 3; -3 1 ];
% X = sampleUnitSphere(2, 100); 
% AX = A*X; 
% figure(1); 
% clf; 
% hold on;
% plot( X(1,:), X(2,:),'b.');
% plot(AX(1,:),AX(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS A')
% 
% B = [1 3; 1 1];
% Y = sampleUnitSphere(2, 100); 
% BY = B*Y; 
% figure(2); 
% clf; 
% hold on;
% plot( Y(1,:), Y(2,:),'b.');
% plot(BY(1,:),BY(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS B')
%  
% C = reshape(1:4,2,2);
% Z = sampleUnitSphere(2, 100); 
% CZ = C*Z; 
% figure(3); 
% clf; 
% hold on;
% plot( Z(1,:), Z(2,:),'b.');
% plot(CZ(1,:),CZ(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS C')

%% askisi 2
% disp('ASKHSH 2');
% B = [1 3; 1 1];
% Y = sampleUnitSphere(2, 100); 
% BY = B*Y; 
% figure(1); 
% clf; 
% hold on;
% plot( Y(1,:), Y(2,:),'b.');
% plot(BY(1,:),BY(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS B')
% 
% D = [-1/2 3/2; 1/2 -1/2];                 %%D=B^-1
% Y = sampleUnitSphere(2, 100); 
% DY = D*Y; 
% figure(2); 
% clf; 
% hold on;
% plot( Y(1,:), Y(2,:),'b.');
% plot(DY(1,:),DY(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS B ANTISTROFOS')
% 
% E = [-1 -3; -1 -1];                 %%E=-B
% Y = sampleUnitSphere(2, 100); 
% EY = E*Y; 
% figure(3); 
% clf; 
% hold on;
% plot( Y(1,:), Y(2,:),'b.');
% plot(EY(1,:),EY(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS -B')

%% askisi 3
% disp('ASKHSH 3');
% A = [-2 3; -3 1 ]
% X = sampleUnitSphere(2, 100)
% 
% for i=A:A*A:A*A*A*A*A*A 
%     for j=0:6
%         iX = i*X; 
%         figure(j); 
%         clf; 
%         hold on;
%         plot( X(1,:), X(2,:),'b.');
%         plot(iX(1,:),iX(2,:),'r.');
%         hold off
%         axis square equal
%         title('PINAKAS A')
%     end
% end

%% askisi 4
% disp('ASKHSH 4');
% A = [1 3 -2; 8 0 1; 2 2 6 ] 
% X = sampleUnitSphere(3, 100); 
% AX = A*X; 
% figure(1); 
% clf; 
% hold on;
% plot( X(1,:), X(2,:),'b.');
% plot(AX(1,:),AX(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS A')
% 
% B = reshape(1:9,3,3)
% X = sampleUnitSphere(3, 100); 
% BX = B*X; 
% figure(2); 
% clf; 
% hold on;
% plot( X(1,:), X(2,:),'b.');
% plot(BX(1,:),BX(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS B')

%% askisi 5
% disp('ASKHSH 5'); 
% A = diag([1 2 3])
% X = sampleUnitSphere(3, 100); 
% AX = A*X; 
% figure(1); 
% clf; 
% hold on;
% plot( X(1,:), X(2,:),'b.');
% plot(AX(1,:),AX(2,:),'r.');
% hold off
% axis square equal
% title('PINAKAS A')

%% askisi 6
% disp('ASKHSH 6'); 
% A = [1 3 -2; 8 0 1; 2 2 6 ]
% B = reshape(1:9,3,3)
% C = A*B
% 
% a = norm(A(:),2)    %11.0905
% b = norm(B(:),2)    %16.8819
% c = norm(C(:),2)    %128.6740
% 
% a*b                 %187.2298
% 
%                   % opote prokyptei c<=a*b h ||AB||<=||A||*||B||

%% 
%%     PROVOLES
% disp('PROVOLES');
%% askisi 1 && 2
% disp('ASKHSH 1 && 2');
% A = [1 0; 0 1; 1 0];
% B = [1 2; 0 1; 1 0];
% a = [1 2 3]';
% s = [1 0 1]';
% 
% % A
% figure(1);
% clf;
% hold on;
% drawVector(a,'r.',{'a'});
% drawVector(s, 'g.')
% 
% P_orth = A*A';
% P_obl = A*[1 0 1; 0 1 0]*inv([A s]);
% a_orth = P_orth *a;
% a_obl = P_obl *a;
% 
% drawVector([a_orth a_obl],'b.',{'a_{orth}','a_{obl}'})
% drawSpan(A)
% hold off
% view (150,15)
% 
% %B
% figure(2);
% clf;
% hold on;
% drawVector(a,'r.',{'a'});
% drawVector(s, 'g.')
% 
% P_orth = B*B';
% P_obl = B*[1 0 1; 2 1 0]*inv([B s]);
% a_orth = P_orth *a;
% a_obl = P_obl *a;
% 
% drawVector([a_orth a_obl],'b.',{'a_{orth}','a_{obl}'})
% drawSpan(B)
% hold off
% view (150,15)

%% 
%%        ANTISTROFOI
% disp ('ANTISTROFOI');
%% askisi 1
% disp('ASKHSH 1');
% A = [1 1; -2 2];
% e1 = [1;0]; e2 = [0;1];
% figure(1); clf; hold on;
% drawVector(A, {'a','b'});
% drawVector([e1 e2], 'gs', {'e_1', 'e_2'})
% hold off;
% 
% % o antistrofos einai A^-1=[1/2 -1/4; 1/2 1/4]

%% askisi 2
%disp('ASKHSH 2');
% A = [1 1; -2 2];
% invA = inv(A)
% P1 = A*diag(invA(:,1));
% P2 = A*diag(invA(:,2));
% 
% figure(1);
% hold on
% drawVector(P1,'rd')
% drawVector(P2,'ms')
% drawLine([e1 P1(:,1)]);
% drawLine([e1 P1(:,2)]);
% drawLine([e2 P2(:,1)]);
% drawLine([e2 P2(:,2)]);
% hold off

%%
%%     PARAGTONTOPOIHSH LU
% disp('PARAGONTOPOIHSH LU');
%% askisi 1
% disp('ASKHSH 1');
% A = [ 2 4; 3 6 ];
% b = [4 7]';

% % xoros stilon
% x = A\b; figure(1);
% clf;
% hold on;
% drawVector(A, {'a_1', 'a_2'})
% drawVector(b, 'rs', {'b'});
% Pb = A*diag(x);
% drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'});
% drawLine([b Pb(:,1)]);
% drawLine([b Pb(:,2)]);
% hold off;
% title('Ax=b in the column space', 'FontSize', 14);
% 
% % xoros grammon
% 
% figure(2);
% clf;
% hold on;
% drawPlane(A(1,:), b(1), 'b');
% drawPlane(A(2,:), b(2), 'g');
% hold off
% title('Ax=b in the row space', 'FontSize', 14);

%% askisi 2
% disp('ASKHSH 2');
% A = [3 12 1; 12 0 2; 0 2 3];
% b = [2.36 5.26 2.77]';
% x = A\b
% Pb = A*diag(x);
% figure(1); clf; hold on;
% drawVector(A, {'a_1','a_2','a_3'});
% drawVector(b, 'rs', {'b'});
% drawLine([b Pb(:,1)]);
% hold off;
% title('Ax=b in the column space', 'FontSize', 14);
% 
% figure(2); clf; hold on;
% drawPlane(A(1,:), b(1), 'b');
% drawPlane(A(2,:), b(2), 'g');
% drawPlane(A(3,:), b(3), 'r');
% plot3(x(1),x(2),x(3), 'mx', 'LineWidth', 4, 'MarkerSize', 15)
% hold off 
% title('Ax=b in the row space', 'FontSize', 14);

%% askisi 3
% disp('ASKHSH 3');
% A = [ 4 1 ; 0 3.5 ];
% b = [2 -3]';
% figure(1); clf; hold on;
% drawPlane(A(1,:), b(1), 'b');
% drawPlane(A(2,:), b(2), 'g');
% hold off
% title('Ax=b in the row space', 'FontSize', 14);
% figure(2);
% hold on;
% plot(x(1), x(2), 'rx', 'MarkerSize', 16, 'LineWidth', 2);
% hold off;
% A = [ 4 1 ; 0 3.5 ]; 
% b = [2 -3]';
% x = A\b;
% figure(2);
% clf; hold on;
% drawVector(A, {'a_1', 'a_2'})
% drawVector(b, 'rs', {'b'});
% Pb = A*diag(x); 
% drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'});
% drawLine([b Pb(:,1)]);
% drawLine([b Pb(:,2)]);
% hold off; 

%% askisi 4
% disp('ASKHSH 4');
% U = [ 2 1 1 0; 0 1 1 1; 0 0 2 2; 0 0 0 2 ];
% I=eye(4); 
% for j=1:4 b = I(1:4,j);
%     x(1:4,j)=backSub(U,b);
% end
% V = U*x;
% display(x); 
% display(V);
%  
% title('Ax=b in the column space', 'FontSize', 14);

%diary off



